<?php

/**
 * 打单系统 Ordering Software
 *
 * (c) cighsen02 <xiayu@959602.com>
 *
 * Create at：2025.05.08
*/

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * 确定TeamId
 */
class SetTeamIdMiddleware
{
    /**
     * 执行TeamId设置, 通过$request->user()当前用户信息
     *
     * @param Request $request
     * @param  Closure(Request): (Response)  $next
     * @param string|null $guard
     * @return Response
     */
    public function handle(Request $request, Closure $next, ?string $guard = null): Response
    {
        $teamId = match ($guard) {
            'user', 'api' => 1, // 前台会员，个人1 企业2
            'admin' => 9, // 后台
            default => 1
        };

        setPermissionsTeamId($teamId);

        return $next($request);
    }
}
